#include"../../include/order.h"

#define TRUE  1
#define FALSE 0

using namespace std;

void GetGateWay(char gateway[])  
{  
    FILE *fp;  
    char buf[512];  
    char cmd[128]; 
    char *tmp;  

    strcpy(cmd, "ip route");  
    fp = popen(cmd, "r"); 
    if(NULL == fp)  
    {  
	perror("popen error");  
	return ;  
    }  
    while(fgets(buf, sizeof(buf), fp) != NULL)  
    {  
	tmp =buf;  
	while(*tmp && isspace(*tmp))  
	    ++ tmp;  
	if(strncmp(tmp, "default", strlen("default")) == 0)  
	    break;  
    }  
    sscanf(buf, "%*s%*s%s", gateway);         
    printf("本地网关:%s\n", gateway);  
    pclose(fp);  
}  



void Get_IP_addr (char ip_address[])
{
    struct ifaddrs * ifAddrStruct=NULL;
    void * tmpAddrPtr=NULL;

    char gateway[30];  
    getifaddrs(&ifAddrStruct);

 
    GetGateWay(gateway);
    
    while (ifAddrStruct!=NULL) 
    {
	if (ifAddrStruct->ifa_addr->sa_family==AF_INET)
	{   // check it is IP4
	    // is a valid IP4 Address
	    tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;
	    char addressBuffer[INET_ADDRSTRLEN];
	    inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
	    if(0==strncmp(gateway,addressBuffer,12))
	    {
		printf("网卡标识:%s\n本地IP地址:%s\n", ifAddrStruct->ifa_name, addressBuffer); 
		strcpy(ip_address,addressBuffer);
		break;
	    }
	}
	ifAddrStruct=ifAddrStruct->ifa_next;
    }

//    return 0;
}

CustomerClient::CustomerClient(){
    Msg.client=CurrentClient;
    OrderList = NULL;
    ChangeList= NULL;
    ServeringStatus=NoServering;
}

CustomerClient::~CustomerClient(){
    delete OrderList;
    delete ChangeList;
}



void CustomerClient::ConnetServer(){
	//************TCP连接*****************//
	int ret;
	//建立套接字，确定要使用的协议家族
	socket_fd = socket(AF_INET,SOCK_STREAM,0);
	if(socket_fd == -1){
	    perror("socket");
	    return ;
	}

	//绑定地址和端口
	ServerSin.sin_family = AF_INET;
	ServerSin.sin_port = htons(ServerPort);
	//ip地址是服务器ip地址
	if(inet_pton(AF_INET,ServerLocalHost,&ServerSin.sin_addr.s_addr) <=0){
	    perror("inet_pton");
	    return ;
	}
	ret = connect(socket_fd,(struct sockaddr*)&ServerSin,sizeof(ServerSin));
	if(ret == -1){
	    perror("连接服务器失败");
	    return ;
	}else{
	    printf("成功连接服务器!\n");
	}

	//*************TCP连接结束*********************//
}

void CustomerClient::SendMessage(){
    Msg.client= CurrentClient;
    Msg.ContentSum=0;
    char buf[1024]={0};

    Content tmp;
    if(Msg.ConnetType==ConnetOrderTable||Msg.ConnetType==ConnetChangeTable){
	auto it=OrderingTable.begin();
	Msg.TableNum=(it->first);	
	memcpy(buf,&Msg,HeaderSize);
	write(socket_fd,buf,1024);
    }else if(Msg.ConnetType==ConnetDeleteFood||Msg.ConnetType==ConnetAddNewFood){
	FoodList *p=ChangeList;
	char buf1[MaxPackSize-HeaderSize];
	while(p!=NULL){
	    ushort pos=Msg.ContentSum*ContentSize;
	    ++Msg.ContentSum;
	    tmp.FoodNum=p->FoodNum;
	    tmp.FoodSum=p->FoodSum;
	    strcpy(tmp.Remark,ChangeList->Remark);
	    memcpy(&buf1[pos],&tmp,ContentSize);
	    p=p->next;
	    if((Msg.ContentSum>=MaxContentSum)&&(p!=NULL)){
		Msg.PackIsEnd =NoEnd;
		memcpy(buf,&Msg,HeaderSize);
		memcpy(&buf[HeaderSize],buf1,MaxPackSize-HeaderSize);
		write(socket_fd,buf,1024);
		Msg.ContentSum=0;
		memset(buf1,0,MaxPackSize-HeaderSize);
	    }
	}
	Msg.PackIsEnd =IsEnd;
	memcpy(buf,&Msg,HeaderSize);
	memcpy(&buf[HeaderSize],buf1,MaxPackSize-HeaderSize);
	write(socket_fd,buf,1024);
	delete ChangeList;
    }else if(Msg.ConnetType==ConnetOrderFood||Msg.ConnetType==ConnetAddFood){
	FoodList *p=OrderList;
	char buf1[RWMaxSize-HeaderSize];
	cout<<"150 send order"<<endl;
	while(p!=NULL){
	    ushort pos=Msg.ContentSum*ContentSize;
	    ++Msg.ContentSum;
	    tmp.FoodNum=p->FoodNum;
	    tmp.FoodSum=p->FoodSum;
	    strcpy(tmp.Remark,p->Remark);
	    memcpy(&buf1[pos],&tmp,ContentSize);
	    p=p->next;
	    if((Msg.ContentSum>=MaxContentSum)&&(p!=NULL)){
		Msg.PackIsEnd =NoEnd;
		memcpy(buf,&Msg,HeaderSize);
		memcpy(buf,buf1,MaxPackSize-HeaderSize);
		write(socket_fd,buf,1024);
		Msg.ContentSum=0;
		memset(buf1,0,MaxPackSize-HeaderSize);
	    }
	}
	cout<<"168 send order"<<endl;
	Msg.PackIsEnd =IsEnd;
	memcpy(buf,&Msg,HeaderSize);
	memcpy(&buf[HeaderSize],buf1,MaxPackSize-HeaderSize);
	write(socket_fd,buf,1024);
	
    }else{
	Msg.PackIsEnd =IsEnd;
	auto it=OrderingTable.begin();
	Msg.TableNum=(it->first);	
	memcpy(buf,&Msg,HeaderSize);
	write(socket_fd,buf,1024);
    }
       cout<<"send Msg->ContentSum="<<Msg.ContentSum<<endl;
       cout<<"send Msg->client="<<Msg.client<<endl;
       cout<<"send Msg->PackIsEnd="<<Msg.PackIsEnd<<endl;
       cout<<"send Msg->ConnetType="<<Msg.ConnetType<<endl;
       cout<<"send Msg->TableNum="<<Msg.TableNum<<endl;
}

void CustomerClient::ReceiveMessage(){
    char buf[RWMaxSize];
    read(socket_fd,buf,RWMaxSize);
    memcpy(&Msg,buf,HeaderSize);
    cout<<"Msg->ContentSum="<<Msg.ContentSum<<endl;
    cout<<"Msg->client="<<Msg.client<<endl;
    cout<<"Msg->PackIsEnd="<<Msg.PackIsEnd<<endl;
    cout<<"Msg->ConnetType="<<Msg.ConnetType<<endl;
    cout<<"Msg->TableNum="<<Msg.TableNum<<endl;
    Content tmp={0,0,0};	
    if(Msg.ContentSum==0)return;
    //-------------------
    //write(0,&tmp,ContentSize);
    //-------------------
    if(Msg.ConnetType==ConnetGetTable||Msg.ConnetType==ConnetChangeTable){	
	int ContentSum=0;
	while(Msg.ContentSum--){
	    memcpy(&tmp,&buf[HeaderSize+ContentSize*ContentSum],ContentSize);
	    ++ContentSum;
	    ServeringTable[tmp.FoodNum]=tmp.Remark;
	}
    }else if(Msg.ConnetType==ConnetGetFood){	
	int ContentSum=0;
	while(Msg.ContentSum--){
	    memcpy(&tmp,&buf[HeaderSize+ContentSize*ContentSum],ContentSize);
	    ++ContentSum;
	    map<ushort,string>maptmp;
	    maptmp[tmp.FoodSum]=tmp.Remark;
	    Servering[tmp.FoodNum]=maptmp;
	}
    }else{	
    }
}


void CustomerClient::MainMenu(){
    cout<<"----------------------------------"<<endl;
    cout<<"1.选桌/换桌"<<endl;
    cout<<"2.点菜"<<endl;
    cout<<"3.加菜/删菜"<<endl;
    cout<<"4.呼叫服务员"<<endl;
    cout<<"5.查看已点菜单"<<endl;
    cout<<"6.结帐"<<endl;
    cout<<"----------------------------------"<<endl;
    cout<<">>";
    string Chose;
    cin>>Chose;
    while(Chose==""||Chose<"1"||Chose>"6"){
	cout<<"选择错误，重新选择"<<endl;
	cout<<">>";
	cin>>Chose;
    }
    if(Chose=="1"){
	OrderTable();
    }else if(Chose=="2"){
	OrderFood();
    }else if(Chose=="3"){
	ChangeFood();
    }else if(Chose=="4"){
	CallWaiter();
    }else if(Chose=="5"){
	ShowMyOrder();
	MainMenu();
    }else if(Chose=="6"){
	Account();
    }else{
	MainMenu();
    }

}


void CustomerClient::OrderTable(){
    if(!OrderingTable.empty()){
	Msg.ConnetType=ConnetChangeTable;    
    }else{
	Msg.ConnetType=ConnetGetTable;    
    }
    map<ushort,string>newtable;
    ServeringTable=newtable;
    SendMessage();
    ReceiveMessage();
    if(ServeringTable.empty()){
	cout<<"当前房间都满了"<<endl;
	MainMenu();
	return;
    }
    cout<<"当前空闲房间"<<endl;
    ShowServeringTable();
    ushort Chose;
    cout<<"您要订哪个房间呢"<<endl;
    while(1){
	cout<<">>";
	cin>>Chose;
	auto it=ServeringTable.find(Chose);
	if(it==ServeringTable.end()){
	    cout<<"不能预定该房间哦，请选择上述的房间编号"<<endl;
	}else{
	    OrderingTable=newtable;
	    OrderingTable[Chose]=it->second;
	    break;
	}
    }
    Msg.ConnetType=ConnetOrderTable;    
    SendMessage();
    ReceiveMessage();
    MainMenu();
}

void CustomerClient::OrderFood(){
    if(OrderingTable.empty()){
	cout<<"还没订房间哦，您先订房间吧"<<endl;
	MainMenu();
	return;
    }
    if(OrderList!=NULL){
	cout<<"您点过菜了"<<endl;
	ChangeFood();
	return;
    }
    if(Servering.empty()){
	Msg.ConnetType=ConnetGetFood;
	SendMessage();
	ReceiveMessage();
    }
    while(1){
	ShowServeringFoodList();
	cout<<"----------------------------"<<endl;
	cout<<"您的拟定菜单如下"<<endl;
	ShowMyOrder();
	string Chose;
	cout<<"输入菜编号(输入ok完成点菜)"<<endl;
	cout<<">>";
	cin>>Chose;
	FoodList *p=OrderList;
	if(Chose=="ok"||Chose=="OK"){
	    if(p){
		Msg.ConnetType=ConnetOrderFood;
		cout<<"send order"<<endl;
		SendMessage();
		cout<<"receive order"<<endl;
		ReceiveMessage();
		cout<<"Onservering"<<endl;
		ServeringStatus=OnServering;
	    }else{
		cout<<"未点任何菜哦"<<endl;
	    }
	    break;
	}
	ushort EnterNum=atoi(Chose.c_str());
	if(Servering.end()==Servering.find(EnterNum)){
	    cout<<"没有这个菜哦，重新选择吧"<<endl;
	    continue;
	}
	if(!p){
		OrderList=new FoodList;
		OrderList->next=NULL;
		cout<<"OrderList==NULL"<<endl;
		p=OrderList;
		p->FoodNum=EnterNum;
		p->FoodSum=1;
	}else{
	    while(p){
		if(p->FoodNum==EnterNum){
		    p->FoodSum++;
		    break;
		}
		if(p->next){
		    p=p->next;
		}else{
		    FoodList *tmp=new FoodList;
		    tmp->FoodNum=EnterNum;
		    tmp->FoodSum=1;
		    p->next=tmp;
		    tmp->pre=p;
		    p=p->next;
		    break;
		}
	    }
	}
    }
    MainMenu();

}

void CustomerClient::ChangeFood(){
    Msg.ConnetType=ConnetCallWaiter;
    if(!OrderList){
	cout<<"您未点菜哦，先点菜吧"<<endl;
	MainMenu();
	return;
    }
    cout<<"您已订的菜单如下"<<endl;
    ShowMyOrder();
    cout<<"您可以:"<<endl;
    cout<<"      1.加菜"<<endl;
    cout<<"      2.删菜"<<endl;
    cout<<"输入其他数字将返回"<<endl;
    string Chose;
    cin>>Chose;
    if(Chose=="1"){
	ChangeList=NULL;
	if(ChangeList==NULL){
	    ChangeList=new FoodList;
	    ChangeList->next=NULL;
	}
	while(1){
	    ShowServeringFoodList();
	    cout<<"您已订的菜单如下"<<endl;
	    ShowMyOrder();
	    cout<<"-----------------------------"<<endl;
	    cout<<"新添加菜单列表"<<endl;
	    ShowChangeList();
	    cout<<"输入加菜编号：(输入ok结束加菜)"<<endl;
	    cout<<">>";
	    cin>>Chose;
	    if(Chose=="ok"||Chose=="OK"){
		Msg.ConnetType=ConnetAddFood;
		SendMessage();
		ReceiveMessage();
		if(ChangeList->next!=NULL){
		    ChangeList=ChangeList->next;
		    Msg.ConnetType=ConnetAddNewFood;
		    SendMessage();
		    ReceiveMessage();
		    FoodList *pp1=OrderList;
		    FoodList *pp2=ChangeList;
		    while(pp1->next){
			pp1=pp1->next;
		    }
		    while(pp2){
			pp1->next=pp2;
			pp2->pre=pp1;
			pp2=pp2->next;
			pp1=pp1->next;
		    }
		    ChangeList->next=NULL;
		    ChangeList=NULL;
		}
		break;
	    }
	    FoodList *p=ChangeList;
	    FoodList *p1=OrderList;
	    ushort EnterNum=atoi(Chose.c_str());
	    if(Servering.end()==Servering.find(EnterNum)){
		cout<<"没有这个菜哦，重新选择吧"<<endl;
		continue;
	    }
	    while(p1){
		if(p1->FoodNum==EnterNum){
		    p1->FoodSum++;
		    break;
		}
		if(!p1->next){
		    while(p){
			if(p->FoodNum==EnterNum){
			    p->FoodSum++;
			    break;
			}
			if(!p->next){
			    FoodList *tmp=new FoodList;
			    tmp->next=NULL;
			    tmp->FoodNum=EnterNum;
			    tmp->FoodSum=1;
			    p->next=tmp;
			    tmp->pre=p;
			    p=p->next;
			    break;
			}
			p=p->next;
		    }
		    break;
		}
		p1=p1->next;
	    }
	}
//	ChangeList=ChangeList->next;

    }else if(Chose=="2"){
	Msg.ConnetType=ConnetDeleteFood;
	ChangeList=NULL;
	while(1){
	    ShowMyOrder();
	    cout<<"-----------------------------"<<endl;
	    cout<<"删除菜单列表"<<endl;
	    ShowChangeList();
	    cout<<"输入删除的菜编号：(输入ok结束删除)"<<endl;
	    cout<<">>";
	    cin>>Chose;
	    if(Chose=="ok"||Chose=="OK"){
		SendMessage();
		ReceiveMessage();
		break;
	    }
	    if(ChangeList==NULL){
		ChangeList=new FoodList;
		ChangeList->next=NULL;
	    }
	    FoodList *p=ChangeList;
	    FoodList *p1=OrderList;
	    ushort EnterNum=atoi(Chose.c_str());
	    while(p1){
	    cout<<"459"<<endl;
		if(p1->FoodNum==EnterNum){
		    cout<<"463"<<endl;
		    p1->FoodSum--;
		    if(p1->FoodSum==0){
			if(p1==OrderList){
			    if(p1->next!=NULL){
				OrderList=OrderList->next;
		//		p1=OrderList;
			    }else{
				OrderList=NULL;
			//	delete OrderList;
				cout<<"472"<<endl;
			    }
			    
			}else{
			    if(p1->next!=NULL){
				p1->pre->next=p1->next;
				p1->next->pre=p1->pre;
				cout<<"469"<<endl;
			    }else{
				p1->pre->next=NULL;
				cout<<"472"<<endl;
			    }
			}
			delete p1;
		    }
		    cout<<"474"<<endl;
		    break;
		}
		cout<<"476"<<endl;
		if(!p1->next){
		    cout<<"您没有点该菜哦"<<endl;
		    p=NULL;
		    break;
		}
		cout<<"480"<<endl;
		p1=p1->next;
	    }
	    cout<<"485"<<endl;
	    while(p){
	    cout<<"487"<<endl;

		if(p->FoodNum==EnterNum){
		    p->FoodSum++;
		    break;
		}
		if(!p->next){
		    FoodList *tmp=new FoodList;
		    tmp->next=NULL;
		    tmp->FoodNum=EnterNum;
		    tmp->FoodSum=1;
		    p->next=tmp;
		    tmp->pre=p;
		    p=p->next;
		    break;

		}
		p=p->next;
	    }
	}

    }else{
	MainMenu();
	return;
    }

    MainMenu();

}

void CustomerClient::CallWaiter(){
    Msg.ConnetType=ConnetCallWaiter;
    cout<<"正在呼叫服务员，请稍等"<<endl;
    SendMessage();
    ReceiveMessage();
    MainMenu();

}


void CustomerClient::Account(){
    if(!OrderList||!ServeringStatus){
	cout<<"您未点菜哦，先点菜，吃饱再结帐吧"<<endl;
	MainMenu();
	return;
    }
    Msg.ConnetType=ConnetAccount;
    SendMessage();
    ReceiveMessage();
    MainMenu();
}

void CustomerClient::ShowServeringFoodList(){
    cout<<"本店菜单"<<endl;
    if(Servering.empty())return;
    for(auto it=Servering.begin();it!=Servering.end();it++){
	auto ii=it->second.begin();
	cout<<"编号："<<it->first<<" "<<ii->second<<" ￥"<<ii->first<<endl;
    }
}


void CustomerClient::ShowMyOrder(){
    if(OrderingTable.empty())return;
    auto it=OrderingTable.begin();
   cout<<"房间："<<it->first<<" "<<it->second<<endl;
    cout<<"点菜清单："<<endl;
    FoodList *p=OrderList;
    while(p){
	//cout<<p->FoodNum<<" "<<p->Remark<<" 份数："<<p->FoodSum<<endl;
	//auto it=Servering.find(p->FoodNum);
	//auto ii=(it->second).begin();
	cout<<p->FoodNum<<" "<<(Servering.find(p->FoodNum)->second).begin()->second<<" 份数："<<p->FoodSum<<endl;
	p=p->next;
    }
}

void CustomerClient::ShowChangeList(){
    FoodList *p =ChangeList;
    if(p){
	p=p->next;
    }
    while(p){
	cout<<p->FoodNum<<" "<<p->Remark<<" 份数："<<p->FoodSum<<endl;
	p=p->next;
    }
}

void CustomerClient::ShowServeringTable(){
    if(!ServeringTable.empty()){
	for(auto it=ServeringTable.begin();it!=ServeringTable.end();it++){
	    cout<<it->first<<" "<<it->second<<endl;
	}
    }
}


int main()
{
    CustomerClient customer;
    customer.ConnetServer();
    customer.MainMenu();
    //customer.SendMessage();
    return 0;
}
